﻿using Catalogo.Portable.DataSources;
using Catalogo.Portable.Model;
using Catalogo.Portable.ViewModel.Commands;
using Catalogo.Portable.ViewModel.Messages;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Catalogo.Portable.ViewModel
{
    public class HomePageViewModel : ViewModelBase
    {
        public ObservableCollection<Categoria> Categorias { get; private set; }
        public ICommand AbrirCategoriaCommand { get; private set; }
        public ICommand AbrirHomeCommand { get; private set; }
        public ICommand AbrirItemCommand { get; private set; }

        private Empresa _Empresa;
        public Empresa Empresa
        {
            get { return _Empresa; }
            set { SetProperty(ref this._Empresa, value); }
        }

        public HomePageViewModel()
        {
            Categorias = new ObservableCollection<Categoria>();
            AbrirCategoriaCommand = new DelegateCommand(OnAbrirCategoriaCommand);
            AbrirHomeCommand = new DelegateCommand(OnAbrirHomeCommand);
            AbrirItemCommand = new DelegateCommand(OnItemHomeCommand);
        }

        private void OnItemHomeCommand(object obj)
        {
            PublishMessage<NavegarParaPagina>(new NavegarParaPagina("ItemPage", obj));
        }

        private void OnAbrirHomeCommand(object obj)
        {
            PublishMessage<NavegarParaPagina>(new NavegarParaPagina("HomePage", obj));
        }

        private void OnAbrirCategoriaCommand(object obj)
        {
            PublishMessage<NavegarParaPagina>(new NavegarParaPagina("CategoriaPage", obj));
        }

        public async Task LoadAsync()
        {
            var categorias = await CatalogoDataSource.Instance.ObterCategoriasComItensAsync(4);
            Categorias.Clear();
            foreach (var categoria in categorias)
            {
                Categorias.Add(categoria);
            }

            Empresa = await CatalogoDataSource.Instance.ObterEmpresaAsync();
        }
    }
}
